package engine.newUi
{
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.flag.ResourcesPath;
	import engine.logic.tips.TipsManage;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	import newx.com.control.window.NxWindowEvent;
	import newx.utils.FilterUtil;

	/**
	 * 
	 * @author zhouqiang
	 * 
	 */	
	public class EveryDaySignLogic
	{
		private var  	 _model           	 :ModelLocator;
		
		private var _bindManage			 :BindManage;
		private static  var _self     	 :EveryDaySignLogic;	
		private static  var 	_ui			 :EveryDaySignUI;
		
		private var _bindIndex				:int	=	-1;
			
		private var textArr:Array;	
		private var btnArr:Array;
		private var  _effectArr:Array;
		
		private var mc1:MovieClip;
		private var mc2:MovieClip;
		private var mc3:MovieClip;
		private var mc4:MovieClip;
		private var mc5:MovieClip;		
		public static function getInstance():EveryDaySignLogic
		{
			if(_self == null)
			{
				_self = new EveryDaySignLogic();		
			}
			return _self;
		}
		
//		public function get ui():EveryDaySignUI
//		{
//			if( _ui == null )
//			{
//				_ui	=	new EveryDaySignUI();
//			}
//			return _ui
//		}
		
		public function EveryDaySignLogic()
		{
		}
		
		public function Init():void
		{   
			_model		=	ModelLocator.getInstance();	
			_bindManage	=	BindManage.getInstance();
			btnArr		=	[];
			textArr		=	[];
			_effectArr	=	[""];
			_ui			=	EveryDaySignUI.getInstance();
//			InitGroup();
		}
		
		public function BeforeShow():void
		{
			_model.addEventListener(
				String(CustomToClientFlag.SERVER_CUSTOMMSG_SIGN_IN_RESULT),getInfoHandle);
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_SIGN_IN_INFO);		
			_ui.windowBG.	addEventListener(	NxWindowEvent.WINDOW_CLOSE, windowCloseHandle);
		}
		
		public function AfterHide():void
		{
			//_bindIndex	=	_bindManage.UnbindProp(_model.scene.Role,"DailySignInCounter",_bindIndex);
		}
		
		/**
		 * 得到签到信息 
		 * @param e
		 * 
		 */		
		private function getInfoHandle(e:CustomMsgEvent):void
		{
			var index:int		=	e.pList[0];	
			var num:int			=	e.pList[1];
			for( var j:int=1;j < _effectArr.length;j++)
			{
				(_effectArr[j] as MovieClip).visible	=	false;
				(_effectArr[j] as MovieClip).gotoAndStop(1);
			}
			for(var i:int = 0;i < _ui.viewGroup.row;i++)
			{
				if(i < num)
				{
					(textArr[i] as TextField).text	=	StaticIni.getSwfLangStr("UIString_1000337");
				}
				else
				{
					(textArr[i] as TextField).text	=	StaticIni.getSwfLangStr("UIString_1000336");
				}
			}
			
			if(index == 0)
			{
				(textArr[num] as TextField).text			=	StaticIni.getSwfLangStr("UIString_1000335");		
				(btnArr[num] as SimpleButton).enabled		=	true;
				(btnArr[num] as SimpleButton).filters		=	[];
				(btnArr[num] as SimpleButton).addEventListener(MouseEvent.CLICK,onMouseClick);
				controlEffect(num);
			}
			else
			{
				(textArr[num-1] as TextField).text			=	StaticIni.getSwfLangStr("UIString_1000337");
				(btnArr[num-1] as SimpleButton).enabled		=	false;
				(btnArr[num-1] as SimpleButton).filters		=	[FilterUtil.GrayColorFilter];
			}
		}
		
		
		private function addEffect():void
		{
			var mc:MovieClip;
			for( var i:int = 0; i < 6 ;i++)
			{
				mc	=	ResManager.newResClass("custSkin_302") as MovieClip;
				mc.gotoAndStop(1);
				mc.visible	=	false;
			//	_ui.viewGroup.addChild(mc);
				_effectArr.push(mc);
			}
		}
		
		private function controlEffect(num:int):void
		{
			var mc:MovieClip;
			mc	=	_ui.viewGroup.getChildAt(num) as MovieClip;
			showEffect(mc,num+1,true);
		}
		
		/**
		 * 初始化显示表格
		 * 
		 */		
		public function InitGroup():void
		{
			var mc:MovieClip;
			var txt1:TextField;
			var btn:SimpleButton;
			addEffect();
			for( var i:int = 1; i < 8; i++ )
			{	
				mc		=	_ui.viewGroup.getChildAt(i-1) 	as MovieClip;
				addinfo(mc,i);
				txt1	=	mc.getChildByName("txt1") 	as TextField;
				btn		=	mc.getChildByName("btn") 	as SimpleButton;
				btn.enabled			=	false;
				btn.filters			=	[FilterUtil.GrayColorFilter];
				txt1.mouseEnabled	=	false;
				textArr.push(txt1);
				btnArr.push(btn);
			}
			_ui.showUI();
		}
		
		private function addinfo(mc:MovieClip,index:int):void
		{		
			mc1		=	mc.getChildByName("p_mc1") as MovieClip;
			mc2		=	mc.getChildByName("p_mc2") as MovieClip;
			mc3		=	mc.getChildByName("p_mc3") as MovieClip;
			mc4		=	mc.getChildByName("p_mc4") as MovieClip;
			mc5		=	mc.getChildByName("p_mc5") as MovieClip;
			// 每个控件有五个要显示的mc块
			for( var i:int = 1; i < 6; i++ )
			{
				var clip:NxClip		=	new NxClip();
				clip.x	=	5;
				clip.y	=	5;
				var configID:String	=	StaticIni.getIniVar("sign_"+index,"photo_"+i);
				if(configID == "")
				{
					(this["mc"+i] as MovieClip).visible	=	false;
					continue;
				}
				
				clip.source			=	ResManager.newResClass(ResourcesPath.getPropsURL(int(configID)));
				
				(this["mc"+i] as MovieClip).addChildAt(clip,1);
				(this["mc"+i] as MovieClip)["data"]	=	StaticIni.getSwfLangStr(StaticIni.getIniVar("sign_"+index,"word_"+i));
				(this["mc"+i].getChildByName("txt1") as TextField).text	=	
									StaticIni.getSwfLangStr(StaticIni.getIniVar("sign_"+index,"word_"+i));
				
				(this["mc"+i].getChildByName("txt2") as TextField) .visible	=	true;
				(this["mc"+i].getChildByName("txt2") as TextField).text	=	
										"+"+StaticIni.getIniVar("sign_"+index,"val_"+i);
			}
			
		}
		
		/**
		 * 给奖励加上特效 
		 * @param mc
		 * @param index
		 * @param isEffect
		 * 
		 */		
		private function showEffect(mc:MovieClip,index:int,isEffect:Boolean = false):void
		{
			for( var i:int = 1; i < _effectArr.length; i++ )
			{
				var configID:String	=	StaticIni.getIniVar("sign_"+index,"photo_"+i);//在这里这是做个标识，
				if(configID == "")return
				(_effectArr[i] as MovieClip).visible	=	isEffect;
				if(isEffect)
				{
					mc.addChild(_effectArr[i] as MovieClip);
					(_effectArr[i] as MovieClip).gotoAndPlay(1);
					(_effectArr[i] as MovieClip).x	=	(this["mc"+i] as MovieClip).x - 24;
					(_effectArr[i] as MovieClip).y	=	(this["mc"+i] as MovieClip).y - 27;
				}
			}	
		}
		
		/**
		 * 鼠标移开
		 * @param e
		 * 
		 */		
		private function onMosueOut(e:MouseEvent):void
		{
			TipsManage.hideTips();
		}
		
		/**
		 *  鼠标移上
		 * @param e
		 * 
		 */		
		private function onMosueOver(e:MouseEvent):void
		{
			var mc:MovieClip	=	e.target as MovieClip;
			var val:String			=	mc["data"];
			TipsManage.showTips(val);
		}
		
		
		/**
		 * 签到
		 * @param e
		 * 
		 */		
		private function onMouseClick(e:MouseEvent):void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_SIGN_IN_REQUEST);
		}
		
		/**
		 * 关闭窗口 
		 * @param e
		 */
		private function windowCloseHandle(e:NxWindowEvent):void
		{
			_ui.showOrHide();
		}
	}
}